// pages/course/course.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    xh: '',
    mm: '',
    cookie: '',

    // 用来保存课表的源码
    c00: {},

    c11: {},
    c12: {},
    c13: {},
    c14: {},
    c15: {},
    c16: {},
    c17: {},

    c31: {},
    c32: {},
    c33: {},
    c34: {},
    c35: {},
    c36: {},
    c37: {},

    c51: {},
    c52: {},
    c53: {},
    c54: {},
    c55: {},
    c56: {},
    c57: {},


    c71: {},
    c72: {},
    c73: {},
    c74: {},
    c75: {},
    c76: {},
    c77: {},


    c91: {},
    c92: {},
    c93: {},
    c94: {},
    c95: {},
    c96: {},
    c97: {},


    c111: {},
    c112: {},
    c113: {},
    c114: {},
    c115: {},
    c116: {},
    c117: {},


    cwk: 11,

    multiArray: [
      ['2018-2019'],
      // ['2018-2019', '2017-2018', '2016-2017', '2015-2016', '2014-2015',],

      ['二'],
      [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24],
      // ['一', '二',],

    ],
    objectMultiArray: [
      [{
          id: 0,
          name: '2018-2019'
        },
        // {
        //   id: 1,
        //   name: '2017-2018'
        // },
        // {
        //   id: 2,
        //   name: '2016-2017'
        // },
        // {
        //   id: 3,
        //   name: '2015-2016'
        // },
        // {
        //   id: 4,
        //   name: '2014-2015'
        // }
      ],
      [
        // {
        //   id: 0,
        //   name: '一'
        // },
        {
          id: 1,
          name: '二'
        }
      ],
      [{
          id: 0,
          name: 1
        },
        {
          id: 1,
          name: 2
        }
      ]
    ],
    multiIndex: [0, 0, 11],
  },

  moreInfo: function(e) {
    // let obj = e.currentTarget.dataset;
    // console.log(obj);
    // let idx = obj.idx;
    // console.log(idx);
    // let str = idx.toString();
    // let course = this.data.str;
    // console.log(course);
    // wx.showModal({
    //   title: course.clsName,
    //   content: '',
    //   showCancel: true,
    //   cancelText: '',
    //   cancelColor: '',
    //   confirmText: '',
    //   confirmColor: '',
    //   success: function(res) {},
    //   fail: function(res) {},
    //   complete: function(res) {},
    // })
  },

  /**
   * 生命周期函数--监听页面加载
   */

  bindMultiPickerChange: function(e) {

    let xn = e.detail.value[0];
    let xq = e.detail.value[1];
    let cwk = e.detail.value[2];

    this.setData({
      multiIndex: e.detail.value
    });
    wx.setStorage({
      key: 'cwk',
      data: cwk,
    })
    let code = wx.getStorageSync('code');
    // let cwk = wx.getStorageSync('cwk');
    this.queryStorageCourse (code, cwk + 1);
  },

  onLoad: function(options) {
    let that = this;
    let local = options.local;
    if (local === '1') {
      wx.showLoading({
        title: '正在加载..',
      });
      let code = wx.getStorageSync('code');
      let cwk = wx.getStorageSync('cwk');
      this.setData({
        multiIndex: [0, 0, cwk],
      })
      this.queryStorageCourse(code, cwk);
      // this.setCourseData(code, cwk);
    } else {
      let xh = options.xh;
      let mm = options.mm;
      let cookie = decodeURIComponent(options.cookie);
      this.setData({
        xh: xh,
        mm: mm,
        cookie: cookie
      })
      let cxn = wx.getStorageSync('cxn');
      let cxq = wx.getStorageSync('cxq');
      // let cwk = wx.getStorageSync('cwk');
      this.setData({
        multiIndex: [0, 0, 0],
      })
      wx.setStorage({
        key: 'cwk',
        data: 0,
      })
      this.queryCourse(cxn, cxq, 1);
    }
  },

  queryStorageCourse: function(code, ckw) {
    let that = this;
    wx.request({
      url: app.globalData.apiAccessSuffix + '/query/course/storage',
      data: {
        code: code,
        wkn: ckw + 1,
      }, 
      method: 'POST',
      dataType: 'json',
      header: {
        'Content-type': 'application/x-www-form-urlencoded',
        'Cookie': that.data.cookie
      },
      success: function(res) {
        let obj = res.data;
        that.setCourseData(obj);
      },
      complete: function() {
        wx.hideLoading();
      }
    });
  },

  queryCourse: function(xn, xq, wk) {
    wx.showLoading({
      title: '正在查询...',
    });

    let that = this;
    let xh = this.data.xh;
    let mm = this.data.mm;
    let cookie = this.data.cookie;

    wx.request({
      url: app.globalData.apiAccessSuffix + '/query/course',
      data: {
        xh: xh,
        mm: mm,
        cookie: cookie,
        xn: xn,
        xq: xq,
        wkn: wk,
      },
      header: {
        'Cookie': that.data.cookie
      },
    
      dataType: 'json',
      success: function(res) {
        let obj = res.data
        // console.log(obj);
        wx.setStorage({
          key: 'hasGetCourse',
          data: true,
        });
        let c00 = obj.c00;
        wx.setStorage({
          key: 'code',
          data: c00.clsName,
        });
        that.setCourseData(obj);
      },
      complete: function() {
        wx.hideLoading();
      }
    });
  },

  setCourseData: function(obj) {
    this.setData({
      c11: obj.c11,
      c12: obj.c12,
      c13: obj.c13,
      c14: obj.c14,
      c15: obj.c15,
      c16: obj.c16,
      c17: obj.c17,


      c31: obj.c31,
      c32: obj.c32,
      c33: obj.c33,
      c34: obj.c34,
      c35: obj.c35,
      c36: obj.c36,
      c37: obj.c37,


      c51: obj.c51,
      c52: obj.c52,
      c53: obj.c53,
      c54: obj.c54,
      c55: obj.c55,
      c56: obj.c56,
      c57: obj.c57,


      c71: obj.c71,
      c72: obj.c72,
      c73: obj.c73,
      c74: obj.c74,
      c75: obj.c75,
      c76: obj.c76,
      c77: obj.c77,


      c91: obj.c91,
      c92: obj.c92,
      c93: obj.c93,
      c94: obj.c94,
      c95: obj.c95,
      c96: obj.c96,
      c97: obj.c97,


      c111: obj.c111,
      c112: obj.c112,
      c113: obj.c113,
      c114: obj.c114,
      c115: obj.c115,
      c116: obj.c116,
      c117: obj.c117,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})